// pages/user/invite/record/record.js
import {
  getInviteMemberRecord
} from '../../../../api/invite.js';
import {
  removeBadge
} from '../../../../api/other.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    vips: [],
    recordList: [],
    pageNo: 1,
    rowCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.vips = getApp().globalData.unRead.invite.vips;
    this.loadRecordList();
  },

  /**
   * 加载记录列表
   */
  loadRecordList() {
    getInviteMemberRecord(this.data.pageNo).then(res => {
      if (res.list.length > 0) {
        // 判断是否显示红点
        res.list = res.list.map(item => {
          if (this.data.vips.includes(item.id)) {
            item.badge = true;
          } else {
            item.badge = false;
          }
          return item;
        })
        this.setData({
          recordList: this.data.recordList.concat(res.list),
          rowCount: res.rowCount,
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.rowCount > this.data.recordList.length) {
      this.data.pageNo += 1;
      this.loadRecordList();
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    getApp().globalData.unRead.invite.vips = [];
    this.data.vips.forEach(item => {
      removeBadge(6, item);
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})